package com.daxidi.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.daxidi.cn.entity.Bill;
import com.daxidi.cn.entity.Pay;
import com.daxidi.cn.mapper.BillMapper;
import com.daxidi.cn.mapper.PayMapper;
import com.daxidi.cn.service.IBillService;
import com.daxidi.cn.vo.BillVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 order 系统默认字段不能用 服务实现类
 * </p>
 *
 * @author wsh
 * @since 2020-11-09
 */
@Service
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements IBillService {
    @Autowired
    private BillMapper billMapper;

    @Autowired
    private PayMapper payMapper;

    @Override
    // 创建订单
    public void addBill(Bill bill) {
        billMapper.insert(bill);
    }

    // 根据订单id删除订单
    @Override
    public void deleteBillById(Long id) {
        //删除关联的pay表记录
        QueryWrapper<Pay> wrapper = new QueryWrapper<>();
        wrapper.eq("bill_id", id);
        List<Pay> pays = payMapper.selectList(wrapper);
        if (pays.size() > 0) {
            for (Pay pay : pays) {
                payMapper.deleteById(pay.getId());
            }
        }
        billMapper.deleteById(id);
    }

    // 根据订单id修改订单
    @Override
    public void updateBillById(Bill bill) {
        billMapper.updateById(bill);
    }

    // 查询所有订单
    @Override
    public Map selectBillsPage(Integer pageNum, Integer pageSize) {
        Page<Bill> page = new Page<>(pageNum, pageSize);
        // 调用mypatis-plus分页查询方法   获取分页后的结果
        Page<Bill> billPage = billMapper.selectPage(page, null);
        // 查询数据
        List<Bill> bills = billPage.getRecords();
        // 总条数
        long total = billPage.getTotal();
        HashMap map = new HashMap();
        // 数据
        map.put("data", bills);
        // 总条数
        map.put("count", total);
        return map;
    }

    // 根据订单id查询订单信息
    @Override
    public Bill selectBillById(Long id) {
        QueryWrapper<Bill> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Bill bill = billMapper.selectOne(wrapper);
        return bill;
    }

    @Override
    public Bill selectBillByMemberId(Long memberId, Long timestamp) {
        QueryWrapper<Bill> wrapper = new QueryWrapper<>();
        wrapper.eq("state", 0);
        wrapper.eq("membe_id", memberId);
        wrapper.orderByDesc("created_at");
        List<Bill> bills = billMapper.selectList(wrapper);
        if (bills.size() > 0) {
            Long createdTime = bills.get(0).getCreatedAt().getTime() + 600000;
            return createdTime > timestamp ? bills.get(0) : null;
        }
        return null;
    }

    // 根据订单详情id查询订单信息
    @Override
    public Bill selectBillByPayId(Long payId) {
        QueryWrapper<Bill> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_id", payId);
        Bill bill = billMapper.selectOne(wrapper);
        return bill;
    }

    // 根据订单号查询订单信息
    @Override
    public Bill selectBillByOrderId(Long orderId) {
        QueryWrapper<Bill> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        Bill bill = billMapper.selectOne(wrapper);
        return bill;
    }

    // 根据商店id查询某年某月的 订单信息
    @Override
    public Map showBillVosByYear(Integer shopId, String year, String month) {
        List<BillVo> billVos = billMapper.showBillVosByYear(shopId, year, month);
        // 应收总金额
        Double totals = 0D;
        for (BillVo b : billVos) {
            Double totall = b.getTotall();
            totals += totall;
        }
        // 实收总金额
        Double discounteds = 0D;
        for (BillVo b : billVos) {
            Double discountedd = b.getDiscountedd();
            discounteds += discountedd;
        }
        Map map = new HashMap();
        map.put("totals", totals);
        map.put("discounteds", discounteds);
        map.put("billVos", billVos);
        return map;
    }

}
